%-------------------------------------------------------------------------;
% Table 4: Predicting the Realized Term Premia;
%-------------------------------------------------------------------------;

clear;
clc;
close all;

%-------------------;
% Import data;
%-------------------;

filename='Main_data_1996_2022';
[data,txt,raw]=xlsread(filename);

dates=txt(2:end,1);

%Index data;
SP_500=data(:,1);         %SP 500 index price level;
Div_m=data(:,2);            %Monthly index dividends;
Div_a=data(:,3);            %Annual index dividends;
SP_ret=data(2:end,4);       %Monthly return on SP 500 index;

%Dividend strip data;
Div_strip_12=data(:,5);     %Price of 12-month dividend strip;
Div_strip_ret=data(2:end,6);%Monthly return on dividend strip strategy;

%Indicated dividends;
Ind_div=data(1:end,7);  

%Bond returns;
Bond_2y=data(2:end,8);      %Monthly return on 2-year Treasury bond;
Bond_10y=data(2:end,9);     %Monthly return on 10-year Treasury bond;

%Fama-French data;
Mkt_Rf=data(2:end,10);      %Market factor;
Rf=data(2:end,11);          %Risk-free rate;

%Define dividend-to-price ratios;
DP_sp=Div_a./SP_500;
DP_strip=Div_a./Div_strip_12;

%Define indicated dividend growthh;
Ind_dg=(Ind_div./Div_a)-1;     

%Take logarithms of all the main variables;
ret_sp=log(SP_ret+1);
ret_strip=log(Div_strip_ret+1);

bond_2y=log(Bond_2y+1);  
bond_10y=log(Bond_10y+1);

mkt_rf=log(Mkt_Rf+1);
rf=log(Rf+1);

dp_sp=log(DP_sp);
dp_strip=log(DP_strip);

ind_dg=log(Ind_dg+1);

%-----------------------------------;
%Predictive regressions;
%-----------------------------------;

%Calculate rho;
rho=(exp(-mean(dp_sp)))/(1+exp(-mean(dp_sp)));

%Take average for dp_strip and start the sample with observation no. 3;
dp_strip_n=filter(ones(3,1),1,dp_strip)./3;

dp_strip=dp_strip_n(3:end);
dp_sp=dp_sp(3:end);
ind_dg=ind_dg(3:end);

ret_sp=ret_sp(3:end);
ret_strip=ret_strip(3:end);

%To store results;
Tab_4=zeros(18,36);

for h=1:36

ret_sp_h=filter(ones(h,1),1,ret_sp);
ret_strip_h=filter(ones(h,1),1,ret_strip);

ret_sp_h=ret_sp_h(h:end);
ret_strip_h=ret_strip_h(h:end);

%Number of lags for Newey-West for overlapping observations;
lags_overlap=h;

%Number of lags for Newey-West for nonoverlapping observations;
lags_nonoverlap=3;

%Adjust the length of the data to given h; 
dp_sp_h=dp_sp(1:(size(ret_sp_h,1)));
dp_strip_h=dp_strip(1:(size(ret_sp_h,1)));
ind_dg_h=ind_dg(1:(size(ret_sp_h,1)));

%------------------------------;
%Dp_sp;
%------------------------------;

y=ret_sp_h-ret_strip_h;

x=[ones(size(y,1),1) dp_sp_h];
[b,~,tb,R2,~,~]=olsnw(y,x,1,lags_overlap);

Tab_4(1:2,h)=[b(2,1);tb(2,1)];
Tab_4(4,h)=R2;

%To store results;
b_non=zeros(2,h);
tb_non=zeros(2,h);

for i=1:h
    y_non=y(i:h:end);
    x_non=x(i:h:end,:);
    [b,~,tb,~,~,~]=olsnw(y_non,x_non,1,lags_nonoverlap);
    b_non(:,i)=b;
    tb_non(:,i)=tb;
end

t_non=mean(tb_non,2);
Tab_4(3,h)=t_non(2,1);

%------------------------------------------;
%Scaled dp_sp - dp_strip;
%------------------------------------------;

%The AR(1) coefficient for the persistence of expected returns is obtained
%from auxiliary regressions of market returns on dividend-price ratio at
%different horizons (details in the paper).;

AR1=0.851;
A=(1-rho*(AR1^(h/12)));

x=[ones(size(y,1),1) A*dp_sp_h-dp_strip_h];
[b,~,tb,R2,~,~]=olsnw(y,x,1,lags_overlap);

Tab_4(6:7,h)=[b(2,1);tb(2,1)];
Tab_4(9,h)=R2;

%To store results;
b_non=zeros(2,h);
tb_non=zeros(2,h);

for i=1:h
    y_non=y(i:h:end);
    x_non=x(i:h:end,:);
    [b,~,tb,~,~,~]=olsnw(y_non,x_non,1,lags_nonoverlap);
    b_non(:,i)=b;
    tb_non(:,i)=tb;
end

t_non=mean(tb_non,2);
Tab_4(8,h)=t_non(2,1);

%-------------------------------------;
%Scaled dp_sp - dp_strip and ind_dg;
%-------------------------------------;

x=[ones(size(y,1),1) A*dp_sp_h-dp_strip_h ind_dg_h];
[b,~,tb,R2,~,~]=olsnw(y,x,1,lags_overlap);

Tab_4(11:12,h)=[b(2,1);tb(2,1)];
Tab_4(14:15,h)=[b(3,1);tb(3,1)];
Tab_4(17,h)=R2;

%To store results;
b_non=zeros(3,h);
tb_non=zeros(3,h);

for i=1:h
    y_non=y(i:h:end);
    x_non=x(i:h:end,:);
    [b,~,tb,~,~,~]=olsnw(y_non,x_non,1,lags_nonoverlap);
    b_non(:,i)=b;
    tb_non(:,i)=tb;
end

t_non=mean(tb_non,2);
Tab_4(13,h)=t_non(2,1);
Tab_4(16,h)=t_non(3,1);
Tab_4(18,h)=length(y);
end

Tab_4=Tab_4(:,[12 18 24 30 36]);

disp('Table 4')
disp(Tab_4)












